package com.sunday.shoufen.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.entity.Income;
import com.sunday.shoufen.http.ApiClient;
import com.sunday.shoufen.ui.mine.CouponListFragment;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/11/8.
 */

public class CouponBalanceActivity extends BaseActivity {


    @Bind(R.id.btn_back)
    ImageView btnBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.my_total_score)
    TextView myTotalScore;
    @Bind(R.id.txt)
    TextView txt;
    @Bind(R.id.slide_tab)
    SlidingTabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    private long memberId;
    private Fragment[] fragments;
    private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_balance);
        ButterKnife.bind(this);
        initView();
        getData();
    }


    private void initView() {
        memberId = BaseApp.getInstance().getMember().getId();
        initFragments();
    }

    private void initFragments() {
        titles.add("获得");
        titles.add("支出");
        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(titles.size());
    }


    @OnClick(R.id.btn_mass_give)
    void  give(){
        intent = new Intent(mContext, SearchMemberActivity.class);
        intent.putExtra("type", 1);
        mContext.startActivity(intent);
    }


    public class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
            fragments = new Fragment[titles.size()];
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments[position] == null) {
                fragments[position] = CouponListFragment.newInstance(position + 1);
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

    }



    private void updateView() {
        if (income == null) {
            return;
        }
        myTotalScore.setText(income.getTicketMoney().setScale(2, RoundingMode.HALF_UP) + "");
    }

    private Income income;

    private void getData() {
        showLoadingDialog(0);
        Call<ResultDO<Income>> call = ApiClient.getApiAdapter().myAssets(memberId, 4, 1);
        call.enqueue(new Callback<ResultDO<Income>>() {
            @Override
            public void onResponse(Call<ResultDO<Income>> call, Response<ResultDO<Income>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    income = response.body().getResult();
                    updateView();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Income>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }
}
